package com.testAll.testlead;

import com.core.comment.StringUtil;
import com.core.jdbc.MySqlSession;
import com.core.jdbc.SqlHelper;
import com.core.jdbc.SqlUtil;

import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class leadFile {
    public static void main(String[] args) throws SQLException {
        long start=new Date().getTime();
        lead();
        System.out.println((new Date().getTime()-start)/1000+"ms");
    }

    public static volatile List<String> sqls = new LinkedList<>();

    public static void lead2() {
        File file = new File("E:\\testabc/0-3古诗/save");
        for (File f : file.listFiles()) {
            System.out.println(f);
        }
    }

    public static void lead() throws SQLException {
        //MySqlSession session=new MySqlSession();
        int grade = 0;
        int tlid = 18;
        String path = "E:/saveNew";
        String root = "/public/";
        File saveNew = new File(path);
        if (!saveNew.exists()) saveNew.mkdir();
        File saveNew2 = new File(path + root);
        if (!saveNew2.exists()) saveNew2.mkdir();

        // 生成存储的地方 遍历复制到存储的地方 遍历生成sql 整体update
        int sort = 0;
        File file = new File("E:\\testabc/0-3古诗/save");
        for (File f : file.listFiles()) {
            System.out.println("name:" + f.getName());
            String name = f.getName().substring(0, f.getName().indexOf("."));
            String type = f.getName().substring(f.getName().indexOf(".") + 1);
            String fpath = root + StringUtil.getUuid() + "." + type;
            String id = StringUtil.getUuid();
            Map<String, Object> par = new HashMap<>();

            String fsql = "insert into accessories \n" +
                    "(acce_id,item_id,file_path,file_name,file_size,upload_date,show_as_image,valid)\n" +
                    "VALUES\n" +
                    "(:acce_id,'public_resource_',:file_path,:file_name,:file_size,now(),0,1);";
            par.put("acce_id", id);
            par.put("item_id", "public_resource_");
            par.put("file_path", fpath);
            par.put("file_name", f.getName());
            par.put("file_size", f.length());
            par.put("show_as_image", 0);
            par.put("filetype", type);
            //SqlHelper.sqlOperate(fsql, par);
            addSql(fsql, par);

            String rsql = "INSERT INTO kcresource\n" +
                    "(rname,usetype,otherid,isdefault,sortnum,sourcetype)\n" +
                    "VALUES\n" +
                    "(:rname,1," + tlid + ",0,:sortnum,3);";
            Map<String, Object> rpar = new HashMap<>();
            sort++;
            rpar.put("rname", name);
            rpar.put("sortnum", sort);
            Long rid = (Long) SqlHelper.sqlOperate(rsql, rpar);
            System.out.println(rid);

            String rssql = "insert into kcresourcesubtable \n" +
                    "(rid,type,materialtype,acid,url,split,sortsub)\n" +
                    "VALUES\n" +
                    "(:rid,1,2,:acid,:url,1,:sortsub);";
            Map<String, Object> rspar = new HashMap<>();
            rspar.put("rid", rid);
            rspar.put("acid", id);
            rspar.put("url", fpath);
            rspar.put("sortsub", sort);

            //session.sqlOperate(rssql,rspar);
            addSql(rssql, rspar);

            String rgsql = "INSERT INTO kcresourcegrade \n" +
                    "(rid,grade)\n" +
                    "VALUES\n" +
                    "(:rid,:grade);";
            Map<String, Object> rgpar = new HashMap<>();
            rgpar.put("rid", rid);
            rgpar.put("grade", grade);
            //session.sqlOperate(rgsql,rgpar);
            addSql(rgsql, rgpar);

            try {
                //将源码写入文件
                FileInputStream is=new FileInputStream(f);
                FileOutputStream os = new FileOutputStream(path+fpath);
                saveFile(is, os);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {

            }

        }

        SqlHelper.sqlOperateList(sqls);
        String usql = "UPDATE\n" +
                "kccourselist cl JOIN kccourse c on cl.cid=c.id and cl.tlid=" + tlid + " JOIN kccoursegrade cg on cg.id=c.cgid and cg.grade=" + grade +
                " JOIN kcresource r on r.usetype=1 and r.otherid=cl.tlid and r.rname=c.code \n" +
                "SET\n" +
                "cl.bgmicid=r.id;";
        SqlHelper.sqlOperate(usql);
        //session.destory();
    }

    private static void addSql(String sql, Map<String, Object> par) {
        sqls.add(SqlUtil.parseSql(sql, par));
    }


    private static void saveFile(InputStream is, OutputStream os) {
        byte[] b = new byte[1024];
        try {

            int length = 0;
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("输出流写入图片失败！");
        } finally {
            try {
                is.close();
                os.close();
                b = null;
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭输出流失败！");
            }
        }
    }

}
